<?php
namespace App\Controller;
use Cake\Cache\Cache;

class UserArosController extends AppController{

    public $_cache_config = 'user_aros';
    public function initialize()
    {
        parent::initialize();
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：获取管理组信息
     * */
    public function index()
    {
        $type = $this->request->getData("type");
        if(!in_array($type, ['allowed', 'denied'])){
            $this->G->error("type_error");
            return null;
        }
        $groups =  $this->UserAros->find()
            ->select(['id', "alias", $type])
            ->order(['id asc'])
            ->toArray();
        foreach ($groups as $key => $value){
            $groups[$key]["acos"] = empty($value[$type]) ? [] : json_decode($value[$type],true);
            $groups[$key][$type] = empty($value[$type]) ? [] : json_decode($value[$type],true);
        }
        $list['list'] = $groups;
        $this->G->success('Success',$list);
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：管理组列表
     * */
    public function lists()
    {
        $groups =  $this->UserAros->find()
            ->select(['id', "alias"])
            ->order(['id asc'])
            ->toArray();
        $list['list'] = $groups;
        $this->G->success('Success',$list);
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：新增管理组
     * */
    public function add()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $alias = $this->request->getData("alias");
        if(empty(trim($alias))){
            $this->G->error("alias_empty");
            return null;
        }
        $groups_db = $this->UserAros->find()
            ->select(["id"])
            ->where(["alias" => $alias])
            ->first();
        if(!empty($groups_db)){
            $this->G->error("alias_exist");
            return null;
        }
        $groups = $this->UserAros->newEntity();
        $groups->alias = $alias;
        $this->UserAros->save($groups);
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：修改管理组
     * */
    public function update($gid = null)
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        if(!is_numeric($gid)){
            $this->G->error("gid_error");
            return null;
        }
        $groups = $this->UserAros->newEntity();
        $alias = $this->request->getData("alias");
        if(!empty(trim($alias))){
            $groups->alias = $alias;
        }
        $type = $this->request->getData("type");
        if(!in_array($type, ['allowed', 'denied'])){
            $this->G->error("type_error");
            return null;
        }
        $acos = $this->request->getData("acos");
        $acos = json_encode($acos,true);
        $groups->$type = $acos;
        $groups_db = $this->UserAros->findById($gid)->select(["id"])->first();
        if(empty($groups_db)){
            $this->G->error("group_not_found");
            return null;
        }
//        if($gid == 1){
//            $this->G->error("group_not_modify");
//            return null;
//        }
        $groups_db = $this->UserAros->find()
            ->select(["id"])
            ->where(["alias" => $alias,"id !=" => $gid])
            ->first();
        if(!empty($groups_db)){
            $this->G->error("alias_exist");
            return null;
        }
        $groups->id = $gid;
        $this->UserAros->save($groups);
        $key = 'user_aros-' . $gid;
        Cache::delete($key,$this->_cache_config);
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：删除管理组
     * */
    public function delete($gid = null)
    {
        if(!is_numeric($gid)){
            $this->G->error("gid_error");
            return null;
        }
        $groups =  $this->UserAros->findById($gid)->select(["id"])->first();
        if(!$groups){
            $this->G->error("group_not_found");
            return null;
        }
        if($gid == 1){
            $this->G->error("group_not_modify");
            return null;
        }
        $this->UserAros->deleteAll(['id'=>$gid]);
        $this->G->success('Success');
    }

}
?>



